package com.ruoyi.meeting.service.impl;

import java.util.List;
import java.util.Date;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.meeting.mapper.SysMeetingMapper;
import com.ruoyi.meeting.mapper.SysMeetingAttendeeMapper;
import com.ruoyi.meeting.domain.SysMeeting;
import com.ruoyi.meeting.domain.SysMeetingAttendee;
import com.ruoyi.meeting.service.ISysMeetingService;

/**
 * 会议管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class SysMeetingServiceImpl implements ISysMeetingService 
{
    @Autowired
    private SysMeetingMapper sysMeetingMapper;

    @Autowired
    private SysMeetingAttendeeMapper sysMeetingAttendeeMapper;

    /**
     * 查询会议管理
     * 
     * @param meetingId 会议管理主键
     * @return 会议管理
     */
    @Override
    public SysMeeting selectSysMeetingByMeetingId(Long meetingId)
    {
        return sysMeetingMapper.selectSysMeetingByMeetingId(meetingId);
    }

    /**
     * 查询会议管理列表
     * 
     * @param sysMeeting 会议管理
     * @return 会议管理
     */
    @Override
    public List<SysMeeting> selectSysMeetingList(SysMeeting sysMeeting)
    {
        return sysMeetingMapper.selectSysMeetingList(sysMeeting);
    }

    /**
     * 新增会议管理
     * 
     * @param sysMeeting 会议管理
     * @return 结果
     */
    @Override
    public int insertSysMeeting(SysMeeting sysMeeting)
    {
        sysMeeting.setCreateTime(DateUtils.getNowDate());
        return sysMeetingMapper.insertSysMeeting(sysMeeting);
    }

    /**
     * 修改会议管理
     * 
     * @param sysMeeting 会议管理
     * @return 结果
     */
    @Override
    public int updateSysMeeting(SysMeeting sysMeeting)
    {
        sysMeeting.setUpdateTime(DateUtils.getNowDate());
        return sysMeetingMapper.updateSysMeeting(sysMeeting);
    }

    /**
     * 批量删除会议管理
     * 
     * @param meetingIds 需要删除的会议管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysMeetingByMeetingIds(Long[] meetingIds)
    {
        // 删除关联的参会人员信息
        for (Long meetingId : meetingIds)
        {
            sysMeetingAttendeeMapper.deleteSysMeetingAttendeeByMeetingId(meetingId);
        }
        return sysMeetingMapper.deleteSysMeetingByMeetingIds(meetingIds);
    }

    /**
     * 删除会议管理信息
     * 
     * @param meetingId 会议管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysMeetingByMeetingId(Long meetingId)
    {
        // 删除关联的参会人员信息
        sysMeetingAttendeeMapper.deleteSysMeetingAttendeeByMeetingId(meetingId);
        return sysMeetingMapper.deleteSysMeetingByMeetingId(meetingId);
    }

    /**
     * 查询今日会议列表
     * 
     * @return 会议列表
     */
    @Override
    public List<SysMeeting> selectTodayMeetingList()
    {
        return sysMeetingMapper.selectTodayMeetingList();
    }

    /**
     * 查询即将开始的会议列表
     * 
     * @return 会议列表
     */
    @Override
    public List<SysMeeting> selectUpcomingMeetingList()
    {
        return sysMeetingMapper.selectUpcomingMeetingList();
    }

    /**
     * 根据会议ID查询参会人员列表
     * 
     * @param meetingId 会议ID
     * @return 参会人员列表
     */
    @Override
    public List<SysMeetingAttendee> selectAttendeesByMeetingId(Long meetingId)
    {
        return sysMeetingAttendeeMapper.selectAttendeesByMeetingId(meetingId);
    }

    /**
     * 添加参会人员
     * 
     * @param sysMeetingAttendee 参会人员
     * @return 结果
     */
    @Override
    public int addAttendee(SysMeetingAttendee sysMeetingAttendee)
    {
        sysMeetingAttendee.setAttendStatus("0"); // 未签到
        return sysMeetingAttendeeMapper.insertSysMeetingAttendee(sysMeetingAttendee);
    }

    /**
     * 会议签到
     * 
     * @param id 参会人员ID
     * @return 结果
     */
    @Override
    public int signIn(Long id)
    {
        return sysMeetingAttendeeMapper.signIn(id);
    }

    /**
     * 统计会议参会人数
     * 
     * @param meetingId 会议ID
     * @return 结果
     */
    @Override
    public int countAttendeesByMeetingId(Long meetingId)
    {
        return sysMeetingAttendeeMapper.countAttendeesByMeetingId(meetingId);
    }

    /**
     * 统计会议签到人数
     * 
     * @param meetingId 会议ID
     * @return 结果
     */
    @Override
    public int countSignedInByMeetingId(Long meetingId)
    {
        return sysMeetingAttendeeMapper.countSignedInByMeetingId(meetingId);
    }

    /**
     * 开始会议
     * 
     * @param meetingId 会议ID
     * @return 结果
     */
    @Override
    public int startMeeting(Long meetingId)
    {
        SysMeeting meeting = new SysMeeting();
        meeting.setMeetingId(meetingId);
        meeting.setMeetingStatus("1"); // 进行中
        return sysMeetingMapper.updateSysMeeting(meeting);
    }

    /**
     * 结束会议
     * 
     * @param meetingId 会议ID
     * @return 结果
     */
    @Override
    public int endMeeting(Long meetingId)
    {
        SysMeeting meeting = new SysMeeting();
        meeting.setMeetingId(meetingId);
        meeting.setMeetingStatus("2"); // 已结束
        return sysMeetingMapper.updateSysMeeting(meeting);
    }
}
